// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "DBManager/Service/Input/Parameter/SETSService.h"

#include "DBManager/Common/ServiceMacro.h"
#include "DBManager/IRepository/Input/Parameter/ISETSRepository.h"

#include "SETSService.h"

using namespace SG::DBManager;

SERVICE_REG_IMPL1 (SETSService, ISETSRepository)

SETSService::SETSService (ISETSRepository& repos) : m_repository (repos)
{}

SG::DataStructure::Common::Status
SETSService::Find (SG::DataStructure::Common::Id subcaseId, SG::DataStructure::Common::Id setId, SG::DataStructure::FEM::SETSData& set)
{
    std::vector<SG::DataStructure::FEM::SETSData> arrs;

    if (m_repository.FindBySId (subcaseId, arrs) == SG::DataStructure::Common::Status::STATUS_Success)
    {
        for (auto&& i : arrs)
        {
            if (i.m_setId == setId)
            {
                set.m_domainId = i.m_domainId;
                set.m_setId    = i.m_setId;
                set.m_sId      = i.m_sId;
                set.m_memeIds.clear ();
                // 处理下THRU的情况
                auto last = 0;
                for (auto&& memId : i.m_memeIds)
                {
                    if (memId > 0)
                    {
                        last = memId;
                        set.m_memeIds.push_back (memId);
                    }
                    else
                    {
                        for (SG::DataStructure::Common::Id num = last + 1; num <= std::abs (memId); num++)
                        {
                            set.m_memeIds.push_back (num);
                        }
                    }
                }

                return SG::DataStructure::Common::Status::STATUS_Success;
            }
        }
    }

    return SG::DataStructure::Common::Status::STATUS_Failure;
}
